package com.yang.controller;
import com.yang.pojo.User;
import com.yang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @RequestMapping("/toRegister")
    public String toRegister(Model model) {
        return "register";
    }
    @RequestMapping("/register")
    public String register(User user) throws UnsupportedEncodingException {
        //访问注册方法
        String name = new String(user.getUserName().getBytes("ISO-8859-1"),"utf-8");
        user.setUserName(name);
        int info = userService.addUser(user);
        return "login";
    }
    @RequestMapping("/toLogin")
    public String toIndex(Model model) {
        return "login";
    }
    @RequestMapping("/login")
    public String loginUser(User user, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        String name = new String(user.getUserName().getBytes("ISO-8859-1"),"utf-8");
        user.setUserName(name);
        User user1= userService.loginUser(user);
        if(user1!=null){
            //登陆成功
            HttpSession session=request.getSession();
            session.setAttribute("user",user1);
            return "indexq";
        }else {
            //登陆失败
            String s="用户名或密码错误";
            model.addAttribute("Error",s);
            return "login";
        }
    }
    @RequestMapping("/toIndex")
    public String toLogin() {
        return "indexq";
    }
    @RequestMapping("/toTest")
    public String toTest(Model model) {
        return "test";
    }
    @RequestMapping("/test")
    public String test(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile file) throws IOException {
        //获取文件名 : file.getOriginalFilename();
        String uploadFileName = "123";
        //如果文件名为空，直接回到首页！
        if ("".equals(uploadFileName)){
            return "redirect:/index.jsp";
        }
        System.out.println("上传文件名 : "+uploadFileName);
        //上传路径保存设置
        String path = request.getRealPath("/upload");
        //如果路径不存在，创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        System.out.println("上传文件保存地址："+realPath);
        InputStream is = file.getInputStream(); //文件输入流
        OutputStream os = new FileOutputStream(new File(realPath,uploadFileName));//文件输出流
        //读取写出
        int len=0;
        byte[] buffer = new byte[1024];
        while ((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
            os.flush();
        }
        os.close();
        is.close();
        return "test";
    }



    @RequestMapping("/toUpdateHead")
    public String toUpdateHead( ) {
        return "updateHead";
    }

    @RequestMapping("/updateHead")
    public String updateHead(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile file) throws IOException {
        HttpSession session=request.getSession();
        User user=(User)session.getAttribute("user");
        System.out.println("头像"+user);
        //获取文件名 : file.getOriginalFilename();
        String uploadFileName = user.getUserName();
        //如果文件名为空，直接回到首页！
        if ("".equals(uploadFileName)){
            return "redirect:/index.jsp";
        }
        System.out.println("上传文件名 : "+uploadFileName);
        //上传路径保存设置
        String path = request.getRealPath("/upload");
        //如果路径不存在，创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        System.out.println("上传文件保存地址："+realPath);
        InputStream is = file.getInputStream(); //文件输入流
        OutputStream os = new FileOutputStream(new File(realPath,uploadFileName));//文件输出流
        //读取写出
        int len=0;
        byte[] buffer = new byte[1024];
        while ((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
            os.flush();
        }
        os.close();
        is.close();
        return "indexq";
    }
}
